package com.sixmac.service.impl;

import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.CMap;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.model.ClienUser;
import com.sixmac.model.ClientClassChapter;
import com.sixmac.model.UserClassChapterRecord;
import com.sixmac.pojo.MyTaskCatalog;
import com.sixmac.pojo.TaskCatalog;
import com.sixmac.service.RecordService;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.*;

/**
 * Created by admin on 2018/7/18.
 */
@Service
public class RecordServiceImpl implements RecordService {
    Blade classChapterService = Blade.create(ClientClassChapter.class);

    //获取目录
    private List<ClientClassChapter> getPid(Integer id) {
        List<ClientClassChapter> recordList = classChapterService.findBy("pid=#{id} order by sort,createtime", CMap.init().put("id", id));
        return recordList;
    }

    public AjaxResult taskIndex(Integer id, ClienUser user) {
        AjaxResult res = new AjaxResult();

        List<TaskCatalog> taskCatalogList = new ArrayList<>();//目录结构

        List<MyTaskCatalog> myTaskCatalogs1 = new ArrayList<>();

        res.setData(CMap.init().put("taskList", myTaskCatalogs1));
        //获取一级目录
        List<ClientClassChapter> recordList = this.getPid(id);
        for (ClientClassChapter classChapter : recordList) {

            TaskCatalog taskCatalog = new TaskCatalog();
            MyTaskCatalog myTaskCatalog = new MyTaskCatalog();
            myTaskCatalog.setId(classChapter.getId());
            myTaskCatalog.setName(classChapter.getName());
            myTaskCatalogs1.add(myTaskCatalog);
            //赋值
            taskCatalog.setName(classChapter.getName());
            myTaskCatalog.setName(classChapter.getName());
            //二级目录
            List<MyTaskCatalog> list = new ArrayList<>();
            myTaskCatalog.setLists(list);
            List<ClientClassChapter> clientClassChapters = this.getPid(classChapter.getId());
            for (ClientClassChapter clientClassChapter : clientClassChapters) {

                MyTaskCatalog taskCatalog1 = new MyTaskCatalog();
                taskCatalog1.setName(clientClassChapter.getName());
                taskCatalog1.setId(clientClassChapter.getId());
                list.add(taskCatalog1);

                List<ClientClassChapter> chapterList = this.getPid(clientClassChapter.getId());
                List<MyTaskCatalog> myTaskCatalogs = new ArrayList<>();
                taskCatalog1.setLists(myTaskCatalogs);

                for (ClientClassChapter chapter : chapterList) {
                    MyTaskCatalog catalog = new MyTaskCatalog();
                    myTaskCatalogs.add(catalog);
                    catalog.setName(chapter.getName());
                    catalog.setType(chapter.getType());
                    catalog.setId(chapter.getId());
                    catalog.setStatus(1);
                    Map m = Db.selectOne(" select * from t_user_class_chapter_record where userId =#{userId} and tid =#{tid} and status =2", CMap.init().put("userId", user.getId()).put("tid", clientClassChapter.getId()));
                    if (m != null) {
                        catalog.setStatus(2);
                    }


                }
                //更新状态
                for (MyTaskCatalog ca : myTaskCatalogs) {
                    taskCatalog1.setStatus(2);
                    if (ca.getStatus() == 1) {
                        taskCatalog1.setStatus(1);
                        break;
                    }

                }

                Map map = new HashMap();
//                map.put("name", clientClassChapter.getName());
//                map.put("type", clientClassChapter.getType());
//                map.put("id", clientClassChapter.getId());
//                map.put("status", 1);
//                Map m = Db.selectOne(" select * from t_user_class_chapter_record where userId =#{userId} and tid =#{tid} and status =2", CMap.init().put("userId", user.getId()).put("tid", clientClassChapter.getId()));
//                if (m != null) {
//                    map.put("status", 2);
//                }
                taskCatalog.getList().add(map);

            }

            //更新状态
            for (MyTaskCatalog ca : list) {
                myTaskCatalog.setStatus(2);
                if (ca.getStatus() == 1) {
                    myTaskCatalog.setStatus(1);
                    break;
                }

            }

            taskCatalog.setOther("2");

//            for (Map m : taskCatalog.getList()) {
//                if (Convert.toInt(m.get("status")) == 1) {
//                    taskCatalog.setOther("1");
//                }
//            }
            taskCatalogList.add(taskCatalog);
        }

        return res;
    }


    private Integer getPidById(Integer pid) {
        ClientClassChapter chapter = Blade.create(ClientClassChapter.class).findById(pid);
        if (chapter.getPid() == 0) {
            return chapter.getId();
        }
        return this.getPidById(chapter.getPid());
    }

    public void record(ClienUser user, Integer id, Integer type) {
        ClientClassChapter chapter = Blade.create(ClientClassChapter.class).findById(id);
        Integer i = this.getPidById(chapter.getPid());
        if (type == 1) {
            this.record1(user, id, i);
        } else {
            this.finshRecord(user, id, i);
        }
    }


    /**
     * 记录用户任务情况
     *
     * @param user
     * @param id
     */
    private void record1(ClienUser user, Integer id, Integer cid) {
        Map map = Db.selectOne("select * from t_user_class_chapter_record where userId=#{userId} and tid=#{id}", CMap.init().put("userId", user.getId()).put("id", id));
        if (map == null) {
            UserClassChapterRecord record = new UserClassChapterRecord();
            record.setUserId(user.getId());
            record.setCreatetime(new Date());
            record.setStatus(1);//未完成
            record.setTid(id);
            record.setPid(cid);
            Blade.create(UserClassChapterRecord.class).save(record);
        }
    }

    private void finshRecord(ClienUser user, Integer id, Integer cid) {
        UserClassChapterRecord userClassChapterRecord = Blade.create(UserClassChapterRecord.class).findFirstBy("userId=#{userId} and tid=#{id}", CMap.init().put("userId", user.getId()).put("id", id));
        if (userClassChapterRecord != null) {
            userClassChapterRecord.setStatus(2);
            Blade.create(UserClassChapterRecord.class).update(userClassChapterRecord);
        } else {
            UserClassChapterRecord record = new UserClassChapterRecord();
            record.setStatus(2);//完成
            record.setPid(cid);
            record.setCreatetime(new Date());
            record.setTid(id);
            record.setUserId(user.getId());
            Blade.create(UserClassChapterRecord.class).save(record);
        }

    }

    private Integer getAll(Integer id) {
        Integer i = 0;
        List<ClientClassChapter> recordList = this.getPid(id);//获取一级目录
        for (ClientClassChapter classChapter : recordList) {
            List<ClientClassChapter> clientClassChapters = this.getPid(classChapter.getId());//二级目录
            for (ClientClassChapter chapter : clientClassChapters) {
                List<ClientClassChapter> clientClassChapters1 = this.getPid(classChapter.getId());//三级目录
                for (ClientClassChapter c : clientClassChapters1) {
                    ++i;
                }
            }


        }
        return i;
    }


    private Integer getMy(Integer userId, Integer id) {
        List<UserClassChapterRecord> recordList = Blade.create(UserClassChapterRecord.class).findBy("userId=#{userId} and pid=#{id} and status = 2", CMap.init().put("userId", userId).put("id", id));
        return recordList.size();
    }


    public String calu(Integer userId, Integer id) {
        Integer total = this.getAll(id);
        Integer userTotal = this.getMy(userId, id);
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(0);
        String result = numberFormat.format((float) userTotal / (float) total * 100);
        return result;
    }


}
